package com.coszero.utils.app

import android.app.Activity
import android.content.Context
import com.coszero.utils.utils.LogX
import java.util.Stack
import kotlin.concurrent.Volatile

/**
 * @author jiyuren
 * @ClassName: net.oschina.app.AppManager
 * @Description: Activity管理类：用于管理Activity和退出程序
 * @date 2014-11-2 上午11:27:55
 * @version 1
 */
class AppManager private constructor() {
    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: Activity) {
        if (activityLs == null) {
            synchronized(AppManager::class.java) {
                if (null == activityLs) {
                    activityLs = Stack()
                    activityLs!!.add(activity)
                }
            }
        } else {
            activityLs!!.add(activity)
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity {
        val activity = activityLs!!.lastElement()
        return activity
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    fun finishActivity() {
        val activity = activityLs!!.lastElement()
        finishActivity(activity)
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity?) {
        if (activity != null) {
            LogX.d("finishActivity = %s", activity.javaClass.simpleName)
            activityLs!!.remove(activity)
            activity.finish()
        }
    }

    /**
     * 移除任务桟
     *
     * @param activity
     */
    fun removeActivity(activity: Activity?) {
        if (null != activity && activityLs!!.contains(activity)) {
            LogX.i("removeActivity = %s", activity.javaClass.simpleName)
            activityLs!!.remove(activity)
        }
    }


    /**
     * 结束指定类
     */
    fun finishActivity(cls: Class<*>) {
        var removeAct: Activity? = null
        for (activity in activityLs!!) {
            if (activity.javaClass == cls) {
                removeAct = activity
            }
        }
        if (null != removeAct) {
            LogX.d("finishActivity = %s", removeAct.javaClass.simpleName)
            finishActivity(removeAct)
        }
    }

    /**
     * 获取指定类名的activity对象
     *
     * @param clazz
     * @return
     */
    fun getActivity(clazz: Class<*>): Activity? {
        for (activity in activityLs!!) {
            if (activity.javaClass == clazz) {
                LogX.d("activity name = %s", activity.javaClass.simpleName)
                return activity
            }
        }
        return null
    }

    /**
     * 结束所有加入列表内的Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = activityLs!!.size
        while (i < size) {
            if (null != activityLs!![i]) {
                activityLs!![i].finish()
            }
            i++
        }
        activityLs!!.clear()
    }

    /**
     * 退出应用程序
     *
     * @param context
     * @param code -1 为非正常退出 0
     */
    fun exitApp(context: Context?, code: Int) {
        try {
            finishAllActivity()
            //            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//            activityMgr.killBackgroundProcesses(context.getPackageName());
//            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(code)
            // System.exit(-1);//非正常关闭
        } catch (e: Exception) {
        }
    }

    companion object {
        var activityLs: Stack<Activity>?=null
        @Volatile
        private var instance: AppManager? = null

        @JvmStatic
        val appManager: AppManager?
            /**
             * 单一实例
             */
            get() {
                if (instance == null) {
                    synchronized(AppManager::class.java) {
                        if (null == instance) {
                            instance = AppManager()
                        }
                    }
                }
                return instance
            }
    }
}